package wwy.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wwy.Common.LogicService;
import wwy.Dao.OrderDao;
import wwy.Dto.OrderItemsDto;
import wwy.Dto.OrdersDto;
import wwy.Dto.CommonDto.UserDto;
import wwy.Entity.CommonEntity.User;
import wwy.Entity.Orders;
import wwy.feign.getMerchantService;
import wwy.feign.getUserService;

import java.util.*;

@Service
public class OrderService extends LogicService<OrderDao, Orders, Long> {
    private final getUserService userService;
    private final OrderItemsService orderItemsService;
    private final getMerchantService merchantService;
    public OrderService(@Autowired OrderDao dao, getUserService userService
                       , OrderItemsService orderItemsService, getMerchantService merchantService) {
        super(dao); this.userService = userService; this.orderItemsService = orderItemsService;
        this.merchantService = merchantService;}

    @Transactional
    public List<OrdersDto> getUserOrders(Long userId) {
        UserDto userDto = userService.getUserById(userId);
        User user = modelMapper.map(userDto, User.class);
        List<Orders> orders = dao.findByUser(user);
        List<OrdersDto> orderDtoS = new ArrayList<>();
        for (Orders order : orders) {
            OrdersDto orderDto = modelMapper.map(order, OrdersDto.class);
            orderDto.setOrderItems(orderItemsService.getOrderItemsByOrderId(order.getId()));
            orderDto.setDeliveryFee(merchantService.getMerchantByName(order.getMerchantName())
                    .getStartDeliveryPrice());
            orderDtoS.add(orderDto);
        }
        return orderDtoS;
    }
    @Transactional
    public void payOrder(Long orderId) {
        Orders order = dao.findById(orderId).orElseThrow(() -> new RuntimeException("订单不存在"));
        order.setStates("已支付");
        dao.save(order);
    }
    @Transactional
    public void addNewOrder(OrdersDto ordersDto) {
        Orders order = new Orders();
        order.setMerchantName(ordersDto.getMerchantName());
        order.setTotalPrice(ordersDto.getTotalPrice());
        order.setStates(ordersDto.getStates());
        order.setUser(modelMapper.map(userService.getUserById(ordersDto.getUserId()), User.class));
        dao.save(order);
    }
    @Transactional
    public Long getNewestOrderId() {
        return dao.findTopByOrderByIdDesc().getId();
    }
    @Transactional
    public void updateOrderItems(OrdersDto ordersDto, Long orderId) {
        List<OrderItemsDto> orderItems = ordersDto.getOrderItems();
        for (OrderItemsDto orderItem : orderItems) {
            orderItemsService.addNewOrderItem(orderItem, orderId, ordersDto.getMerchantName());
        }
    }

    // 添加默认订单
    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    @Order(1)
    public void addDefaultOrders(){
        if(dao.count() > 0) return;
        Orders order = new Orders();
        order.setMerchantName("万家饺子（软件园E18店）");
        order.setTotalPrice(49.0);
        order.setStates("未支付");
        UserDto userDto = userService.getUserById(3L);
        User user = modelMapper.map(userDto, User.class);
        order.setUser(user);
        dao.save(order);

        Orders order1 = new Orders();
        order1.setMerchantName("万家饺子（软件园E18店）");
        order1.setTotalPrice(34.0);
        order1.setStates("已支付");
        User user1 = modelMapper.map(userDto, User.class);
        order1.setUser(user1);
        dao.save(order1);

        Orders order2 = new Orders();
        order2.setMerchantName("小锅饭豆腐馆（全运店）");
        order2.setTotalPrice(36.5);
        order2.setStates("未支付");
        User user2 = modelMapper.map(userDto, User.class);
        order2.setUser(user2);
        dao.save(order2);

        Orders order3 = new Orders();
        order3.setMerchantName("小锅饭豆腐馆（全运店）");
        order3.setTotalPrice(44.5);
        order3.setStates("已支付");
        User user3 = modelMapper.map(userDto, User.class);
        order3.setUser(user3);
        dao.save(order3);
    }

}
